package com.junwell.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junwell.common.dto.PageDto;
import com.junwell.common.lang.PageResult;
import com.junwell.entity.Manager;
import com.junwell.entity.User;
import com.junwell.mapper.ManagerMapper;
import com.junwell.mapper.UserMapper;
import com.junwell.service.ManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;



    //分页显示
    @Transactional
    @Override
    public PageResult findPage(PageDto pageDto) {
        Page<Manager> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        String query = pageDto.getQueryString();
        if (StringUtils.hasLength(query)){
            queryWrapper.or().eq("login_name",pageDto.getQueryString());
            queryWrapper.or().like("login_name",pageDto.getQueryString());
        }
        List<Manager> managers = managerMapper.selectList(queryWrapper);
        return new PageResult(page.getTotal(), managers);
    }

    //添加管理员
    @Transactional
    @Override
    public boolean add(Manager manager) {
        int i = managerMapper.insert(manager);
        return  i>0? true:false;
    }



    @Transactional
    @Override
    public Manager viewOne(Integer id) {
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Manager manager = managerMapper.selectOne(queryWrapper);
        return manager;
    }

    @Transactional
    @Override
    public boolean update(Manager manager) {
        int i = managerMapper.updateById(manager);
        return i>0? true:false;
    }

    @Transactional
    @Override
    public boolean delete(Integer id) {
        QueryWrapper<Manager> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        int i = managerMapper.delete(queryWrapper);
        return i>0? true:false;
    }
}
